package P_2023_03_08_ArrayList;

import java.util.ArrayList;
import java.util.Scanner;

public class arrayList {
    public static void main(String[] args) {
        //1.集合
        //test1();

        //2.添加学生对象练习
        //test2();

        //3.查找用户 id
        //test3();

        //4.手机类
        test4();


    }

    //1.集合
    public static void test1() {
        ArrayList<String> list = new ArrayList<>();

        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");

        System.out.print("[");
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                System.out.print(list.get(i));
            } else {
                System.out.print(list.get(i) + ", ");
            }
        }
        System.out.println("]");
    }

    //2.添加学生对象练习
    public static void test2() {
        ArrayList<Student> list = new ArrayList<>();

       /* Student s1 = new Student("张三", 23);
        Student s2 = new Student("李四", 24);
        Student s3 = new Student("王五", 25);*/

        //键盘录入
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            Student stu = new Student();
            System.out.println("请输入学生的姓名：");
            stu.setName(sc.next());
            System.out.println("请输入学生的年龄：");
            stu.setAge(sc.nextInt());

            list.add(stu);
        }

        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getName() + ", " + stu.getAge());
        }
    }

    //3.查找用户 id
    public static void test3() {
        ArrayList<User> list = new ArrayList<>();

        User u1 = new User("001", "张三", "123");
        User u2 = new User("002", "李四", "1234");
        User u3 = new User("003", "王五 ", "123456");
        list.add(u1);
        list.add(u2);
        list.add(u3);

        boolean flag = contains(list, "001");
        System.out.println(flag);
    }

    public static boolean contains(ArrayList<User> list, String id) {
       /* for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return true;
            }
        }
        return false;*/
        return getIndex(list, id) >= 0;
    }

    public static int getIndex(ArrayList<User> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    //4.手机类
    public static void test4() {
        //1.创建集合
        ArrayList<Phone> list = new ArrayList<>();

        Phone p1 = new Phone("小米", 1000);
        Phone p2 = new Phone("苹果", 8000);
        Phone p3 = new Phone("锤子", 2999);

        list.add(p1);
        list.add(p2);
        list.add(p3);

        ArrayList<Phone> result = getPhoneInfo(list);
        for (int i = 0; i < result.size(); i++) {
            Phone p = result.get(i);
            System.out.println(p.getBrand() + ", " + p.getPrice());
        }
    }

    public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> list) {
        ArrayList<Phone> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Phone p = list.get(i);
            if (p.getPrice() < 3000) {
                result.add(p);
            }
        }
        return result;
    }


}
